package me.passtime.projt.account;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import me.passtime.projt.Main;
import me.passtime.projt.R;
import me.passtime.projt.util.AlertDialogUtils;
import me.passtime.projt.util.CheckUtils;
import me.passtime.projt.util.PageJumpUtils;
import me.passtime.projt.util.SystemUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-7
 * Time: 下午3:03
 */
public class RetrievePasswordStepOnePage extends Activity implements View.OnClickListener {
    private EditText mRId;
    private EditText mVCode;
    private EditText mMsgCode;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 4;
    private RetrievePasswordController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.retrieve_pwd_one);

        mRId = (EditText) this.findViewById(R.id.retrieve_pwd_reg_id);
        mVCode = (EditText) this.findViewById(R.id.retrieve_pwd_reg_id);
        mMsgCode = (EditText) this.findViewById(R.id.retrieve_pwd_vf_code);

        this.findViewById(R.id.retrieve_pwd_submit_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_get_btn).setOnClickListener(this);

        this.findViewById(R.id.retrieve_pwd_back_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_logout_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_my_account_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_fee_records_btn).setOnClickListener(this);
        this.findViewById(R.id.retrieve_pwd_exit).setOnClickListener(this);

        mController=new RetrievePasswordController(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retrieve_pwd_submit_btn:
                submitUserInfo();
                break;
            case R.id.retrieve_pwd_back_btn:
                PageJumpUtils.jump(this, Main.class);
                break;
            case R.id.retrieve_pwd_logout_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.retrieve_pwd_my_account_btn:
                PageJumpUtils.jumpToManageAccountPage(this);
                break;
            case R.id.retrieve_pwd_fee_records_btn:
                PageJumpUtils.jumpToQueryPaymentPage(this);
                break;
            case R.id.retrieve_pwd_exit:
                AlertDialogUtils.showYesNoDiaLog(this, "是否退出系统?", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        SystemUtils.killSelf();
                        finish();
                    }
                });
                break;
            case R.id.retrieve_pwd_get_btn:
                sendMsgVerifiedCode();
                break;
        }
    }

    private void sendMsgVerifiedCode() {
        String regId = mRId.getText().toString().trim();
        String code = mVCode.getText().toString().trim();
        showDialog(DIALOG_PROGRESS);
        mController.sendVerificationCode(regId,code);
    }

    private void submitUserInfo() {
        String rid = mRId.getText().toString();
        String vfCode = mVCode.getText().toString();
        String msgCode = mMsgCode.getText().toString();
        if ("".equals(rid) || "".equals(vfCode) || "".equals(msgCode)) {
            AlertDialogUtils.showConfirmDiaLog(this, "请完整填写");
            return;
        }
        if(!CheckUtils.isMobileNo(rid)){
            AlertDialogUtils.showConfirmDiaLog(this, "请填写正确的手机号");
            return;
        }
        Intent intent = new Intent(this, RetrievePasswordStepThreePage.class);
        intent.putExtra("regId", rid);
        intent.putExtra("code",vfCode);
        intent.putExtra("msgCode", msgCode);
        this.startActivity(intent);
//        mController.submitUserInfo(rid,vfCode,msgCode);

    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Main.class);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
